package com.campus.delivery.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.delivery.bean.DeliveryApplyBean;
import com.campus.delivery.entity.DeliveryApply;
import com.campus.delivery.mapper.DeliveryApplyMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 配送员申请表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-05-12
 */
@Service
public class DeliveryApplyService extends ServiceImpl<DeliveryApplyMapper, DeliveryApply> {
    /**
     * 获取指定配送员的最新申请记录
     */
    public DeliveryApply getLatestByUserId(Integer deliveryUserId) {
        LambdaQueryWrapper<DeliveryApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeliveryApply::getDeliveryUserId, deliveryUserId)
                .orderByDesc(DeliveryApply::getCreateTime)
                .last("limit 1");
        List<DeliveryApply> list = this.list(wrapper);
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * 提交或重新提交入驻申请
     * @param bean 入参对象
     * @return 新申请记录ID
     */
    public Integer submitApply(DeliveryApplyBean bean) {
        DeliveryApply apply = new DeliveryApply();
        BeanUtil.copyProperties(bean, apply);
        this.save(apply);
        return apply.getId();
    }
}
